<template>
  <div class="app-container">

    <!-- 查询条件 -->
    <el-form size="small" :inline="true" :model="queryParam" class="demo-form-inline">
      <el-form-item label="登录账户">
        <el-input v-model="queryParam.loginName" placeholder="登录账户" />
      </el-form-item>

      <el-form-item label="性别">
        <el-select v-model="queryParam.gender" clearable placeholder="性别">
          <el-option
            v-for="item in sexList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="queryParam.status" clearable placeholder="状态">
          <el-option
            v-for="item in stateList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="btnSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd()">新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate()">修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini">导出</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData.rows" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="id" label="编号" width="80" />
      <el-table-column prop="loginName" label="账户" width="180" />
      <el-table-column label="性别" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender===1" type="primary">男</el-tag>
          <el-tag v-else type="success">女</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="状态" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status===0" type="primary">正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="头像" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" width="150">
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      v-if="tableData.total>0"
      :current-page="queryParam.page"
      :page-sizes="[1, 5, 10, 20,30,50,100]"
      :page-size="queryParam.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 新增和修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :before-close="cancelForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="登录名" prop="loginName">
          <el-input v-model="form.loginName" placeholder="请输入登录名称(唯一)" />
        </el-form-item>

        <el-form-item v-if="!form.id" label="密码" prop="loginPwd">
          <el-input v-model="form.loginPwd" type="password" placeholder="请输入密码" />
        </el-form-item>

        <el-form-item v-if="!form.id" label="确认密码" prop="reLoginPwd">
          <el-input v-model="form.reLoginPwd" type="password" placeholder="请再次输入密码" />
        </el-form-item>

        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio
              v-for="dict in sexList"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in stateList"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :headers="headers"
            :action="imageUploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.avatar" :src="form.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>

          <el-input type="hidden" v-model="form.avatar"></el-input>

        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelForm">取 消</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import { commonDataApi } from '@/api/commonData.js'
import { dataListApi, findByIdApi, addApi, editApi } from '@/api/employee.js'
import PageMinix from '@/mixin/PageMixin.js'
import AddOrEditMixin from '@/mixin/AddOrEditMixin.js'
export default {
  mixins: [PageMinix, AddOrEditMixin],
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.loginPwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      fetchDataApi: dataListApi,
      findByIdApi: findByIdApi,
      addApi: addApi,
      editApi: editApi,
      sexList: [],
      stateList: [],
      primaryKey: 'id',
      rules: {
        loginName: [
          { required: true, message: '该值不能为空', trigger: 'blur' }
        ],
        loginPwd: [
          { required: true, message: '该值不能为空', trigger: 'blur' }
        ],
        reLoginPwd: [
          { validator: validatePass,   trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '该值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initFormValue()
    this.fetchCommonData()
  },
  methods: {
    fetchCommonData() {
      commonDataApi().then(resp => {
        this.sexList = resp.data.sexData
        this.stateList = resp.data.stateData
      })
    },
    // 表单初始化值
    initFormValue() {
      this.form = {
        avatar: undefined,
        gender: 0,
        status: 0
      }
    },
    // 图片上传返回解析
    handleAvatarSuccess(res, file) {
      console.log(res)
      if (res.code !== 200) {
        this.$message.error(res.msg)
      } else {
        this.form.avatar = 'http://localhost:8080' + res.data.image
      }
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
